package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_CHANGE_FLAG, desc = "修改联盟旗帜")
public class ChangeUnionFlagCmd extends AbstractCommand {

    private int unionFlag;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionProto.UnionModifyFlagReqMsg reqMsg = UnionProto.UnionModifyFlagReqMsg.parseFrom(packet.getBytes());
        unionFlag = reqMsg.getUnionFlag();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        if(null == unionInfo){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        if (unionInfo.getMasterUserId() != player.getUserId()) {//权限不足
            ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
            return;
        }

        player.sendPacket(Protocol.C_UNION_CHANGE_FLAG, UnionProto.UnionModifyFlagReqMsg.newBuilder().setUnionFlag(unionFlag));
        TonBingMgr.unionChange(unionUid);
        ChannelTonBingMgr.unionChange(unionUid);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionProto.UnionChangeRandomJoinRespMsg.Builder respMsg = UnionProto.UnionChangeRandomJoinRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_CHANGE_FLAG, respMsg);
        }
    }
}
